﻿<%@ page language="java" 
    contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import = "java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>查询所有用户信息</title>
</head>
<body>
<h3>所有用户信息</h3>
   	<table border="1">
      <tr>
		<th>用户名</th>
		<th>密  码</th>
      </tr>
<%
    // 加载数据库驱动，注册到驱动管理器
    Class.forName("com.mysql.jdbc.Driver");

	// 数据库连接
    String url = "jdbc:mysql://localhost:3306/user2";
    String username = "root";
    String password = "root";
    Connection conn = DriverManager.getConnection(url,username,password);
    
	// 获取数据库操作对象
// 	Statement stmt = conn.createStatement();
	
	// 构造sql语句
	String sql = "select * from user_b";
	PreparedStatement pstmt = conn.prepareStatement(sql);
	
	// 执行sql语句
// 	ResultSet rs = stmt.executeQuery(sql);
	ResultSet rs = pstmt.executeQuery();
	
    // 结果处理（将查询结果输出到表格中，一条记录占一行）
    String uname = null;
    String upassword = null;
	while(rs.next()){
		uname = rs.getString("uname");
		upassword = rs.getString("upassword");
%>
		<tr>
			<td><%=uname %></td>
			<td><%=upassword %></td>
		</tr>
<%	
	}
	// 释放资源
	if(rs != null){ rs.close();}
	if(pstmt != null){ pstmt.close();}
	if(conn != null){ conn.close();}
%>
  </table>
</body>
</html>